<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license    GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );
//$file = JPATH_COMPONENT.DS.'helpers'.DS.'gdext.php';
//var_dump($file);
require_once(JPATH_COMPONENT.DS.'helpers'.DS.'gdext.php');
//var_dump(get_defined_constants());
//require_once('/var/www/rc3/components/com_calendar/helpers/gdext.php');
/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class eventlistModelEditcatcss extends JModel
{
    var $_data;
	 var $_buildtype;
	 var $_db;
	 var $_com_params;
	 var $_cat_data;
	
	 
	 function __construct()
	{
		
		parent::__construct();
		$this->_db = JFactory::getDBO();
	}
	
	
    /**
     * Retrieves the hello data
     * @return array Array of objects containing the data from the database
     */
    function getData()
    {
		// $this->com_data =& JComponents::getInfo('com_calendar');
		$this->_cat_data =& $this->getCatParams();
		//var_dump($this->_cat_data);
		if(count($this->_cat_data > 0)){
			foreach($this->_cat_data as $id){
				$reg = new JRegistry('cat_data_params');
				$reg->loadINI($id->params,'cat_data_params');
				$obj =& $reg->toObject('cat_data_params');
				foreach($obj as $key=>$value){
					$id->$key = $value;
				}//end foreach
			}//end foreach
		}//end if
		$this->_com_params =& JComponentHelper::getParams('com_eventlist');		
    }
	
	function getCatParams(){
		//TODO: refactory to use model/table and check for errors
		$query = "SELECT id,catname,params from #__eventlist_categories";
		return $this->_getList( $query );
	}
	///////////////////////////////////////
	function mkPng($id,$hexcolor = 'ffffff'){
		
		$file = JPATH_SITE.DS.'components'.DS.'com_eventlist'.DS.'assets'.DS.'images'.DS.'bg'.$id.'.png';
		$r = 3;
		$m = 5;
		$w = 102;
		$h = 60+ (2 * $r)+ (2 * $m);
		$draw =& new imageDraw($r,$m,$w,$h);
		if ($draw->canvas){
			$draw->mkPoints();
			//$hexcolor = '#335522';
			$draw->mkCorners( $draw->setColor($draw->canvas,$hexcolor) );
			$draw->mkPoly8( $draw->setColor( $draw->canvas,$hexcolor) );
			header("Content-type: image/png");
			imagepng($draw->canvas, $file);
		}//end if
	}
	
	function store(){
		//store params for component
		
		//$db_data = $this->getData();
		$data	= JRequest::get( 'post' );
		$params	= JRequest::getVar( 'params', '', 'post', 'array' );
		
		$registry = new JRegistry( 'com_eventlist' );
		$registry->loadArray($params,'editcatcss');
	//	$registry->loadArray($params['cat_id'],'cal_config.cat_id');
		
		$table = '#__eventlist_categories';
		$keyName = 'id';
		foreach($params['cat_id'] as $key=>$value){
			$object->id = $key; //int
			$reg_data = 'cat_id'.$key; //string
			$registry->loadArray($value,$reg_data); //populate namespace with array
			$object->params = $registry->toString('INI',$reg_data); //extract ini string
			//$this->update->$key = $value;
			if(!$this->_db->updateObject( $table,  $object,  $keyName)){
				echo $this->db->stderr();
				return false;
			}//end of if
			$this->mkPng($object->id, $value['event_bg']);
		}//end foreach
		return true;
	}	//end store()
}//end config model
?>
